/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : SortingUtil.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Nov 30, 2008       Raghu Sarraff			       Created
 */
package net.lwf.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import net.lwf.number.util.RandomNumberGenerator;
import net.lwf.string.util.StringManipulator;




/**
 * Type the class description here
 *  
 */
public class SortingUtil {
	
	
	public static int[] sortInts(List lstData, boolean isAscending) {
		int[] sorted = new int[lstData.size()];
	    Collections.sort(lstData);
	    int size = lstData.size();
	    if(isAscending) {
		    for(int i=0; i<size; i++) {
		    	sorted[i] = ((Integer) lstData.get(i)).intValue();
		    }	    	
	    }
	    else {
		    for(int i=0; i<size; i++) {
		    	sorted[i] = ((Integer) lstData.get(size-i-1)).intValue();
		    }	    	
	    }
	    

		return sorted;
	} //sortInts
	
	public static String[] sortStrings(List lstData) {
		return null;
	}
	
	public static int[] sort(int[] data, boolean ascending) {
		Arrays.sort(data);
		if(ascending) {
			return data;
		}
		else {
			int[] newdata = new int[data.length];
			
            int nCounter = data.length;
            int nLoop = 0;
            while(nCounter > 0) {
            	newdata[nLoop] = data[nCounter-1];
            	nLoop++;
            	nCounter--;
            }
			
//			for(int i=data.length; i>=1; i--) {
//				newdata[i-1] = data[i];
//			}
			return newdata;
		}
	}

	/**
	 * Sorts the input java.util.List of Strings by length.
	 * 
	 * @param lstdata
	 * 
	 * @return
	 */
	public static List sortByLength(List lstdata) {
		List sorteddata = null;
		if(lstdata != null) {
			SortedMap mapdata = new TreeMap();
			for(int i=0; i<lstdata.size(); i++) {
				String data = (String)lstdata.get(i);
				Integer key = new Integer(data.trim().length());
				String sLen = String.valueOf(data.trim().length());
				List lData = null;
				if(mapdata.containsKey(key)) {
					lData = (List) mapdata.get(key);
				}
				else {
					lData = new ArrayList();
				}
				lData.add(data);
				mapdata.put(key, lData);
			}
			if(sorteddata == null) {
				sorteddata = new ArrayList();
			}
			Iterator iterator = mapdata.entrySet().iterator();
			while(iterator.hasNext()) {
				Map.Entry mapentry = (Map.Entry) iterator.next();
				List worddata = (List) mapentry.getValue();
				Collections.sort(worddata);
				for(int i=0; i<worddata.size(); i++) {
					sorteddata.add(worddata.get(i));
				}
			}
		}
		return sorteddata;
	}
	
	public static List shuffle(List lstData) {
		List newlist = null;
		if(lstData != null && lstData.size() != 0) {
			newlist = new ArrayList(lstData.size());
			int[] data = RandomNumberGenerator.generateInts(0, lstData.size()-1, lstData.size());
			for(int i=0; i<data.length; i++) {
				newlist.add(lstData.get(data[i]));
			}
		}
		return newlist;
	}
	
	public static void main(String[] args) {
		List lstData = RandomNumberGenerator.generateInts(2, 10);
		System.out.println("=======> Ascending");
		PrintUtil.print(sortInts(lstData, true));
		System.out.println("=======> Descending");
		PrintUtil.print(sortInts(lstData, false));	
		
		System.out.println("===> String is: " + StringManipulator.generateString(lstData));
		List lstdata = new ArrayList();
		lstdata.add("raghu");
		lstdata.add("raj");
		lstdata.add("ra");
		lstdata.add("rama");
		lstdata.add("apple");
		lstdata.add("ape");
		lstdata.add("am");
		lstdata.add("an");
		lstdata.add("a");
		lstdata.add("reciprocate");
		lstdata.add("respect");
		
		lstdata = SortingUtil.sortByLength(lstdata);
		for(int i=0; i<lstdata.size(); i++) {
			System.out.println(lstdata.get(i));
		}
	}

}

